<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="java.io.*"%>
<%
// 访问地址格式：http://localhost:8080/com.sundaying.gooway/admin/toHtml.jsp?file_name=admin/index&todir=html
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";

	// 输入文件 路径
	String toDir = request.getParameter("todir") == null ? "" : request
			.getParameter("todir");
	String fileName = request.getParameter("file_name");

	ServletContext sc = pageContext.getServletContext();
	RequestDispatcher rd = sc.getRequestDispatcher("/" + fileName
			+ ".jsp");

	// 读取jsp文件 返回的stream
	final ByteArrayOutputStream os = new ByteArrayOutputStream();
	final ServletOutputStream stream = new ServletOutputStream() {
		public void write(byte[] data, int offset, int length) {
			os.write(data, offset, length);
		}

		public void write(int b) throws IOException {
			os.write(b);
		}
	};

	// 调用JSP文件，返回stream
	final PrintWriter pw = new PrintWriter(new OutputStreamWriter(os));
	HttpServletResponse rep = new HttpServletResponseWrapper(response) {
		public ServletOutputStream getOutputStream() {
			return stream;
		}

		public PrintWriter getWriter() {
			return pw;
		}
	};
	rd.include(request, rep);
	pw.flush();

	// 将转换后的文件 保存到服务器上的绝对路径
	toDir = request.getRealPath(toDir) + "\\";
	//把jsp输出的内容写到xxx.htm
	FileOutputStream fos = new FileOutputStream(toDir + fileName + ".html");
	os.writeTo(fos);
	fos.close();

	// 输出链接
	out.print("basePath: " + basePath);
	out.print("<br/>fileName: " + fileName);
	out.print("<br/>toDir: " + toDir);
	out.print("<div> <a href=\"" + basePath + fileName + ".html\">"
			+ basePath + fileName + ".html</a></div> ");
// 	out.print("保存至：" + toDir + fileName + ".html");

// 	response.sendRedirect(basePath + fileName + ".html");
%>